package me.hl.proxy;

import com.google.common.reflect.Reflection;

import java.lang.reflect.Proxy;

/**
 * Created by pulangke on 2017/5/10.
 */
public class InvocationHandlerChain {


    public static Object invokeChain(MyFilter[] myFilters, Class[] interfaces, Class targetClass) throws Exception {
        Object target = targetClass.newInstance();
        for (MyFilter myFilter : myFilters) {
            target = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, new MyInvocation(target, myFilter));
        }
        return target;
    }

    public static Object invokeChainGuava(MyFilter[] myFilters, Class interfaces, Class targetClass) throws Exception {
        Object object = targetClass.newInstance();
        for (MyFilter myFilter : myFilters) {
            object = Reflection.newProxy(interfaces, new MyInvocation(object, myFilter));
        }
        return object;

    }
}
